package org.mult.daap;

import org.mult.daap.background.DBAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class AddServerMenu extends Activity {
   private EditText serverNameEditText;
   private EditText serverAddressEditText;
   private EditText serverPortEditText;
   private EditText passwordEditText;
   private CheckBox loginCheckBox;
   private Button okButton;
   private Button cancelButton;
   private DBAdapter db;
   Builder builder;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.add_server_menu);
      builder = new AlertDialog.Builder(this);
      db = new DBAdapter(this);
      okButton = (Button) findViewById(R.id.serverOkButton);
      okButton.setEnabled(false);
      cancelButton = (Button) findViewById(R.id.serverCancelButton);
      serverNameEditText = (EditText) findViewById(R.id.serverNameText);
      serverAddressEditText = (EditText) findViewById(R.id.serverUrlText);
      serverPortEditText = (EditText) findViewById(R.id.serverPortText);
      loginCheckBox = (CheckBox) findViewById(R.id.loginCheckBox);
      passwordEditText = (EditText) findViewById(R.id.serverPasswordText);
      serverAddressEditText.addTextChangedListener(new tw());
      loginCheckBox.setOnCheckedChangeListener(new loginRequiredListener());
      okButton.setEnabled(false);
      passwordEditText.setEnabled(false);
      okButton.setOnClickListener(new AddServerButtonListener());
      cancelButton.setOnClickListener(new cancelButtonListener());
   }

   private class tw implements TextWatcher {
      public void afterTextChanged(Editable s) {
         if (s.length() != 0) {
            okButton.setEnabled(true);
         } else {
            okButton.setEnabled(false);
         }
      }

      public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
         // TODO Auto-generated method stub

      }

      public void onTextChanged(CharSequence s, int start, int before, int count) {
      }
   };

   private class loginRequiredListener implements OnCheckedChangeListener {

      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if (isChecked == true) {
            passwordEditText.setEnabled(true);
         } else {
            passwordEditText.setEnabled(false);
         }
      }
   };

   private class AddServerButtonListener implements OnClickListener {
      public void onClick(View v) {
         String serverName = serverNameEditText.getText().toString();
         String serverAddress = serverAddressEditText.getText().toString();
         String password = passwordEditText.getText().toString();
         String port = serverPortEditText.getText().toString();
         if (port.equals("")) {
            serverPortEditText.setText("3689");
            port = "3689";
         }
         if (serverName.equals("") || serverAddress.equals("")) {
            builder.setTitle(R.string.error_title);
            builder.setMessage(R.string.add_server_error_message);
            builder.setPositiveButton(android.R.string.ok, null);
            builder.show();
            return;
         }
         if (loginCheckBox.isChecked() == true && password.equals("")) {
            builder.setTitle(R.string.error_title);
            builder.setMessage(R.string.add_server_error_message);
            builder.setPositiveButton(android.R.string.ok, null);
            builder.show();
            return;
         }
         serverAddress = serverAddress + ":" + port;
         db.open();
         db.insertServer(serverName, serverAddress, password, loginCheckBox
               .isChecked());
         db.close();
         setResult(RESULT_OK);
         finish();
      }
   }

   private class cancelButtonListener implements OnClickListener {

      public void onClick(View v) {
         setResult(RESULT_CANCELED);
         finish();
      }
   }
}